#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int a[200005];
ll dp[1000005];
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(false);cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        ll sum=0,mx=0;
        cin>>n;
        for(int i=1;i<=n;i++)cin>>a[i];
        memset(dp,~0x3f,sizeof dp);
        dp[0]=0;
        for(int i=2;i<=n;i++){
            ll tmp=max(mx,dp[a[i]]+a[i]);
            if(a[i]==a[i-1])sum+=a[i],tmp-=a[i];
            dp[a[i-1]]=max(dp[a[i-1]],tmp);
            mx=max(mx,tmp);
        }
        cout<<sum+mx<<'\n';
    }
}
